package com.wzt.lianfirecontrol.adapter.xiaokongshi;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.activity.ShowFragActivity;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.function.xiaokongshi.XksRecordItemModel;
import com.wzt.lianfirecontrol.dao.MyHandler;
import com.wzt.lianfirecontrol.utils.GildeUtils;
import com.wzt.lianfirecontrol.utils.MinioUtils;
import com.wzt.lianfirecontrol.utils.ScreenUtils;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.ToastUtils;
import com.wzt.lianfirecontrol.utils.Utils;

public class RecordItemAdapter extends BaseRecyclerAdapter<XksRecordItemModel> {

    private BaseActivity activity;

    private boolean isUpdateImg = false;

    private RecordItemCallBack callBack;

    private boolean isDetail = false;

    private MyHandler handler;

    public MyHandler getHandler() {
        return handler;
    }

    public void setHandler(MyHandler handler) {
        this.handler = handler;
    }

    public boolean isUpdateImg() {
        return isUpdateImg;
    }

    public void setUpdateImg(boolean updateImg) {
        isUpdateImg = updateImg;
    }

    public boolean isDetail() {
        return isDetail;
    }

    public void setDetail(boolean detail) {
        isDetail = detail;
    }

    public RecordItemCallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(RecordItemCallBack callBack) {
        this.callBack = callBack;
    }

    public RecordItemAdapter(BaseActivity activity) {
        this.activity = activity;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(activity).inflate(R.layout.item_duty_detail_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, XksRecordItemModel data) {
        if (viewHolder instanceof MyViewHolder){
            setMyData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout ll_item_content;
        private TextView tv_duty_detail_item_title;
        private TextView tv_item_remark;
        private TextView tv_item_status;
        private LinearLayout ll_item_choose;
        private TextView tv_yichang;
        private TextView tv_zhengchang;
        private RelativeLayout rl_duty_detail_item_content;
        private EditText et_duty_detail_item_content;
        private MyTextWatcher myTextWatcher;
        private ImageView iv_duty_detail_item_play_photo;
        private View include_play_photo_three;
        private RelativeLayout rl_device_photo_1;
        private ImageView iv_device_photo_1;
        private ImageView iv_photo_delete_1;
        private RelativeLayout rl_device_photo_2;
        private ImageView iv_device_photo_2;
        private ImageView iv_photo_delete_2;
        private RelativeLayout rl_device_photo_3;
        private ImageView iv_device_photo_3;
        private ImageView iv_photo_delete_3;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            initMyView(this, itemView);
        }
    }

    private void initMyView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.tv_duty_detail_item_title = itemView.findViewById(R.id.tv_duty_detail_item_title);
        myViewHolder.tv_item_remark = itemView.findViewById(R.id.tv_item_remark);
        myViewHolder.tv_item_status = itemView.findViewById(R.id.tv_item_status);
        myViewHolder.ll_item_choose = itemView.findViewById(R.id.ll_item_choose);
        myViewHolder.tv_yichang = itemView.findViewById(R.id.tv_yichang);
        myViewHolder.tv_zhengchang = itemView.findViewById(R.id.tv_zhengchang);
        myViewHolder.rl_duty_detail_item_content = itemView.findViewById(R.id.rl_duty_detail_item_content);
        myViewHolder.et_duty_detail_item_content = itemView.findViewById(R.id.et_duty_detail_item_content);
        myViewHolder.myTextWatcher = new MyTextWatcher();
        myViewHolder.et_duty_detail_item_content.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus){
                    myViewHolder.et_duty_detail_item_content.addTextChangedListener(myViewHolder.myTextWatcher);
                }else {
                    myViewHolder.et_duty_detail_item_content.removeTextChangedListener(myViewHolder.myTextWatcher);
                }
            }
        });


        myViewHolder.iv_duty_detail_item_play_photo = itemView.findViewById(R.id.iv_duty_detail_item_play_photo);
        myViewHolder.include_play_photo_three = itemView.findViewById(R.id.include_play_photo_three);
        myViewHolder.rl_device_photo_1 = itemView.findViewById(R.id.rl_device_photo_1);
        myViewHolder.iv_device_photo_1 = itemView.findViewById(R.id.iv_device_photo_1);
        myViewHolder.iv_photo_delete_1 = itemView.findViewById(R.id.iv_photo_delete_1);
        myViewHolder.rl_device_photo_2 = itemView.findViewById(R.id.rl_device_photo_2);
        myViewHolder.iv_device_photo_2 = itemView.findViewById(R.id.iv_device_photo_2);
        myViewHolder.iv_photo_delete_2 = itemView.findViewById(R.id.iv_photo_delete_2);
        myViewHolder.rl_device_photo_3 = itemView.findViewById(R.id.rl_device_photo_3);
        myViewHolder.iv_device_photo_3 = itemView.findViewById(R.id.iv_device_photo_3);
        myViewHolder.iv_photo_delete_3 = itemView.findViewById(R.id.iv_photo_delete_3);

        int width = (ScreenUtils.getScreenWidth(activity) - Utils.dip2px(activity, 90)) / 3;
        myViewHolder.rl_device_photo_1.getLayoutParams().width = width;
        myViewHolder.rl_device_photo_1.getLayoutParams().height = width;
        myViewHolder.rl_device_photo_2.getLayoutParams().width = width;
        myViewHolder.rl_device_photo_2.getLayoutParams().height = width;
        myViewHolder.rl_device_photo_3.getLayoutParams().width = width;
        myViewHolder.rl_device_photo_3.getLayoutParams().height = width;
    }

    private void setMyData(MyViewHolder viewHolder, XksRecordItemModel data, int position) {
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();
        if (0 == position){
            layoutParams.setMargins(0, 0, 0, 0);
            viewHolder.ll_item_content.setPadding(0, 0, 0, Utils.dip2px(activity, 15));
        }else if (getItemCount() - 1 == position){
            layoutParams.setMargins(0, activity.getResources().getDimensionPixelSize(R.dimen.list_interval), 0, 0);
            viewHolder.ll_item_content.setPadding(0, Utils.dip2px(activity, 15), 0, 0);
        }else {
            layoutParams.setMargins(0, activity.getResources().getDimensionPixelSize(R.dimen.list_interval), 0, 0);
            viewHolder.ll_item_content.setPadding(0, Utils.dip2px(activity, 15), 0, Utils.dip2px(activity, 15));
        }

        viewHolder.tv_duty_detail_item_title.setText(data.getItemName());

        if (isDetail){
            // 详情
            viewHolder.tv_item_remark.setVisibility(View.VISIBLE);
            viewHolder.tv_item_status.setVisibility(View.VISIBLE);
            viewHolder.ll_item_choose.setVisibility(View.GONE);
            viewHolder.rl_duty_detail_item_content.setVisibility(View.GONE);
            if (StringUtils.isEmpty(data.getRemark())){
                viewHolder.tv_item_remark.setText("");
            }else {
                viewHolder.tv_item_remark.setText(data.getRemark());
            }
            if ("0".equals(data.getStatus())) {
                // 异常
                viewHolder.tv_item_status.setText("异常");
                viewHolder.tv_item_status.setTextColor(activity.getResources().getColor(R.color.yichang_color));
                viewHolder.include_play_photo_three.setVisibility(View.VISIBLE);
                updateImageView(viewHolder, data);
            } else {
                // 正常
                viewHolder.tv_item_status.setText("正常");
                viewHolder.tv_item_status.setTextColor(activity.getResources().getColor(R.color.main_color));
                viewHolder.include_play_photo_three.setVisibility(View.GONE);
            }

        }else {
            viewHolder.tv_item_remark.setVisibility(View.INVISIBLE);
            viewHolder.tv_item_status.setVisibility(View.GONE);
            viewHolder.ll_item_choose.setVisibility(View.VISIBLE);
            viewHolder.rl_duty_detail_item_content.setVisibility(View.VISIBLE);

            if ("0".equals(data.getStatus())) {
                // 异常
                viewHolder.ll_item_choose.setBackground(activity.getResources().getDrawable(R.mipmap.icon_duty_detail_yichang));
                viewHolder.tv_zhengchang.setTextColor(activity.getResources().getColor(R.color.hint_text_color));
                viewHolder.tv_yichang.setTextColor(activity.getResources().getColor(R.color.yichang_color));
                viewHolder.rl_duty_detail_item_content.setVisibility(View.VISIBLE);
                viewHolder.include_play_photo_three.setVisibility(View.VISIBLE);

            } else {
                // 正常
                viewHolder.ll_item_choose.setBackground(activity.getResources().getDrawable(R.mipmap.icon_duty_detail_zhengchang));
                viewHolder.tv_zhengchang.setTextColor(activity.getResources().getColor(R.color.main_color));
                viewHolder.tv_yichang.setTextColor(activity.getResources().getColor(R.color.hint_text_color));
                viewHolder.rl_duty_detail_item_content.setVisibility(View.GONE);
                viewHolder.et_duty_detail_item_content.setText("");
                viewHolder.include_play_photo_three.setVisibility(View.GONE);
            }

            updateImageView(viewHolder, data);

            viewHolder.ll_item_choose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if ("1".equals(data.getStatus())) {
                        viewHolder.ll_item_choose.setBackground(activity.getResources().getDrawable(R.mipmap.icon_duty_detail_yichang));
                        viewHolder.tv_zhengchang.setTextColor(activity.getResources().getColor(R.color.hint_text_color));
                        viewHolder.tv_yichang.setTextColor(activity.getResources().getColor(R.color.yichang_color));
                        viewHolder.rl_duty_detail_item_content.setVisibility(View.VISIBLE);
                        viewHolder.include_play_photo_three.setVisibility(View.VISIBLE);
                        data.setStatus("0");
                    } else {
                        viewHolder.ll_item_choose.setBackground(activity.getResources().getDrawable(R.mipmap.icon_duty_detail_zhengchang));
                        viewHolder.tv_zhengchang.setTextColor(activity.getResources().getColor(R.color.main_color));
                        viewHolder.tv_yichang.setTextColor(activity.getResources().getColor(R.color.hint_text_color));
                        viewHolder.rl_duty_detail_item_content.setVisibility(View.GONE);
                        viewHolder.et_duty_detail_item_content.setText("");
                        viewHolder.include_play_photo_three.setVisibility(View.GONE);

                        data.setStatus("1");
                    }
                    String[] imgUrls = data.getImgUrls().split(",");
                    for (int i = 0; i < imgUrls.length; i++) {
                        MinioUtils.removeFile(activity, imgUrls[i], handler);
                    }
                    data.setImgUrls("");
                    updateImageView(viewHolder, data);
                }
            });
        }

        viewHolder.myTextWatcher.setPosition(position);
        viewHolder.et_duty_detail_item_content.setText(data.getRemark());


        viewHolder.iv_device_photo_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] imgUrls = data.getImgUrls().split(",");
                if (imgUrls.length > 0 && imgUrls[0].startsWith("http") || isDetail) {
                    ((ShowFragActivity) activity).setShowImg(imgUrls[0]);
                } else {
                    callBack.addImageOrPlayPhoto(position + 1000, false);
                }
            }
        });

        viewHolder.iv_photo_delete_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg) {
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                String[] imageUrls = data.getImgUrls().split(",");
                MinioUtils.removeFile(activity, imageUrls[0], handler);
                imageUrls[0] = "";
                data.setImgUrls(updateImageUrl(imageUrls));
                updateImageView(viewHolder, data);
            }
        });

        viewHolder.iv_device_photo_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] imageUrls = data.getImgUrls().split(",");
                if (imageUrls.length > 1 || isDetail) {
                    ((ShowFragActivity) activity).setShowImg(imageUrls[1]);
                } else {
                    callBack.addImageOrPlayPhoto(position + 2000, false);
                }
            }
        });

        viewHolder.iv_photo_delete_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg) {
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                String[] imageUrls = data.getImgUrls().split(",");
                MinioUtils.removeFile(activity, imageUrls[1], handler);
                imageUrls[1] = "";
                data.setImgUrls(updateImageUrl(imageUrls));
                updateImageView(viewHolder, data);
            }
        });

        viewHolder.iv_device_photo_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] imageUrls = data.getImgUrls().split(",");
                if (imageUrls.length > 2 || isDetail) {
                    ((ShowFragActivity) activity).setShowImg(imageUrls[2]);
                } else {
                    callBack.addImageOrPlayPhoto(position + 3000, false);
                }
            }
        });

        viewHolder.iv_photo_delete_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg) {
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                String[] imageUrls = data.getImgUrls().split(",");
                MinioUtils.removeFile(activity, imageUrls[2], handler);
                imageUrls[2] = "";
                data.setImgUrls(updateImageUrl(imageUrls));
                updateImageView(viewHolder, data);
            }
        });
    }

    private String updateImageUrl(String[] imageUrls){
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < imageUrls.length; i++){
            if (!StringUtils.isEmpty(imageUrls[i])){
                if (!StringUtils.isEmpty(stringBuilder.toString())){
                    stringBuilder.append(",");
                }
                stringBuilder.append(imageUrls[i]);
            }
        }
        return stringBuilder.toString();
    }

    private void updateImageView(MyViewHolder myViewHolder, XksRecordItemModel data) {
        RelativeLayout rl_device_photo_1 = myViewHolder.rl_device_photo_1;
        ImageView iv_device_photo_1 = myViewHolder.iv_device_photo_1;
        ImageView iv_photo_delete_1 = myViewHolder.iv_photo_delete_1;
        RelativeLayout rl_device_photo_2 = myViewHolder.rl_device_photo_2;
        ImageView iv_device_photo_2 = myViewHolder.iv_device_photo_2;
        ImageView iv_photo_delete_2 = myViewHolder.iv_photo_delete_2;
        RelativeLayout rl_device_photo_3 = myViewHolder.rl_device_photo_3;
        ImageView iv_device_photo_3 = myViewHolder.iv_device_photo_3;
        ImageView iv_photo_delete_3 = myViewHolder.iv_photo_delete_3;
        String[] imgUrl = data.getImgUrls().split(",");
        int size = 0;
        for (int i = 0; i < imgUrl.length; i++) {
            if (!StringUtils.isEmpty(imgUrl[i]) && imgUrl[i].startsWith("http")) {
                size++;
            }
        }
        switch (size) {
            case 3:
                rl_device_photo_1.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_1, imgUrl[0]);
                iv_photo_delete_1.setVisibility(View.VISIBLE);

                rl_device_photo_2.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_2, imgUrl[1]);
                iv_photo_delete_2.setVisibility(View.VISIBLE);

                rl_device_photo_3.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_3, imgUrl[2]);
                iv_photo_delete_3.setVisibility(View.VISIBLE);
                break;
            case 2:
                rl_device_photo_1.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_1, imgUrl[0]);
                iv_photo_delete_1.setVisibility(View.VISIBLE);

                rl_device_photo_2.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_2, imgUrl[1]);
                iv_photo_delete_2.setVisibility(View.VISIBLE);
                if (isDetail) {
                    rl_device_photo_3.setVisibility(View.INVISIBLE);
                } else {
                    rl_device_photo_3.setVisibility(View.VISIBLE);
                }
                iv_device_photo_3.setImageResource(R.mipmap.icon_duty_detail_photo_big);
                iv_photo_delete_3.setVisibility(View.INVISIBLE);
                break;
            case 1:
                rl_device_photo_1.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_1, imgUrl[0]);
                iv_photo_delete_1.setVisibility(View.VISIBLE);

                if (isDetail) {
                    rl_device_photo_2.setVisibility(View.INVISIBLE);
                } else {
                    rl_device_photo_2.setVisibility(View.VISIBLE);
                }
                iv_device_photo_2.setImageResource(R.mipmap.icon_duty_detail_photo_big);
                iv_photo_delete_2.setVisibility(View.INVISIBLE);

                rl_device_photo_3.setVisibility(View.INVISIBLE);
                break;
            case 0:
                iv_device_photo_1.setImageResource(R.mipmap.icon_duty_detail_photo_big);
                iv_photo_delete_1.setVisibility(View.INVISIBLE);

                if (isDetail) {
                    rl_device_photo_1.setVisibility(View.INVISIBLE);
                } else {
                    rl_device_photo_1.setVisibility(View.VISIBLE);
                }
                rl_device_photo_2.setVisibility(View.INVISIBLE);

                rl_device_photo_3.setVisibility(View.INVISIBLE);
                break;
        }

        if (isDetail) {
            iv_photo_delete_1.setVisibility(View.INVISIBLE);
            iv_photo_delete_2.setVisibility(View.INVISIBLE);
            iv_photo_delete_3.setVisibility(View.INVISIBLE);
        }
    }

    private class MyTextWatcher implements TextWatcher {

        private int position;

        public int getPosition() {
            return position;
        }

        public void setPosition(int position) {
            this.position = position;
        }

        public MyTextWatcher() {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            getmDatas().get(position).setRemark(s.toString());
        }
    }

    public interface RecordItemCallBack{
        void addImageOrPlayPhoto(int what, boolean isPlayPhoto);
    }

}
